package com.yoyosys.common.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.yoyosys.common.util.CommonUtil;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 接口响应数据
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T>  implements Serializable {
    private static final long serialVersionUID = -4185151304730686014L;
    public static final String CODE_SUCCESS = "SUCCESS";
    public static final String CODE_FAIL = "400";
    public static final String CODE_ERROR = "500";

    private String code;

    private String msg;

    private String timestamp;

    private T data;

    public Result(){
        timestamp = CommonUtil.formatDate(new Date(), CommonUtil.FORMAT_DATETIME);
    }
    public static Result success() {
        Result result = new Result();
        result.setCode(CODE_SUCCESS);
        result.setMsg("Success");
        return result;
    }

    public static Result success(String msg) {
        Result result = new Result();
        result.setCode(CODE_SUCCESS);
        result.setMsg(msg);
        return result;
    }

    public static Result success(Object object) {
        Result result = new Result();
        result.setData(object);
        result.setCode(CODE_SUCCESS);
        result.setMsg("Success");
        return result;
    }
    public static Result success(String msg, String key, Object object) {
        Result result = new Result();
        Map<String, Object> data = new HashMap<>();
        data.put(key, object);
        result.setData(data);
        result.setCode(CODE_SUCCESS);
        result.setMsg(msg);
        return result;
    }
    public static Result success(String msg, Object object) {
        Result result = new Result();
        result.setData(object);
        result.setCode(CODE_SUCCESS);
        result.setMsg(msg);
        return result;
    }

    public static Result fail(String msg) {
        Result result = new Result();
        result.setMsg(msg);
        result.setCode(CODE_FAIL);
        return result;
    }

    public static Result error(String msg) {
        Result result = new Result();
        result.setCode(CODE_ERROR);
        result.setMsg(msg);
        return result;
    }

    public static Result error(String code, String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }


}
